package com.ruoyi.business.service.impl;

import com.ruoyi.business.domain.EquipmentVariables;
import com.ruoyi.business.mapper.EquipmentVariablesMapper;
import com.ruoyi.business.service.IEquipmentVariablesService;
import com.ruoyi.business.vo.StripTemMonitorVO;
import com.ruoyi.common.utils.CommentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 存储设备变量参数信息的Service业务层处理
 * 
 * @author ldkj
 * @date 2025-02-27
 */
@Service
public class EquipmentVariablesServiceImpl implements IEquipmentVariablesService 
{
    @Autowired
    private EquipmentVariablesMapper equipmentVariablesMapper;


    /**
     * 查询存储设备变量参数信息的
     * 
     * @param variableId 存储设备变量参数信息的主键
     * @return 存储设备变量参数信息的
     */
    @Override
    public EquipmentVariables selectEquipmentVariablesByVariableId(Long variableId)
    {
        return equipmentVariablesMapper.selectEquipmentVariablesByVariableId(variableId);
    }

    /**
     * 查询存储设备变量参数信息的列表
     * 
     * @param equipmentVariables 存储设备变量参数信息的
     * @return 存储设备变量参数信息的
     */
    @Override
    public List<EquipmentVariables> selectEquipmentVariablesList(EquipmentVariables equipmentVariables)
    {
        return equipmentVariablesMapper.selectEquipmentVariablesList(equipmentVariables);
    }

    /**
     * 新增存储设备变量参数信息的
     * 
     * @param equipmentVariables 存储设备变量参数信息的
     * @return 结果
     */
    @Override
    public int insertEquipmentVariables(EquipmentVariables equipmentVariables)
    {
        return equipmentVariablesMapper.insertEquipmentVariables(equipmentVariables);
    }

    /**
     * 修改存储设备变量参数信息的
     * 
     * @param equipmentVariables 存储设备变量参数信息的
     * @return 结果
     */
    @Override
    public int updateEquipmentVariables(EquipmentVariables equipmentVariables)
    {

        return equipmentVariablesMapper.updateEquipmentVariables(equipmentVariables);
    }

    /**
     * 批量删除存储设备变量参数信息的
     * 
     * @param variableIds 需要删除的存储设备变量参数信息的主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentVariablesByVariableIds(Long[] variableIds)
    {
        return equipmentVariablesMapper.deleteEquipmentVariablesByVariableIds(variableIds);
    }

    /**
     * 删除存储设备变量参数信息的信息
     * 
     * @param variableId 存储设备变量参数信息的主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentVariablesByVariableId(Long variableId)
    {
        return equipmentVariablesMapper.deleteEquipmentVariablesByVariableId(variableId);
    }

    /**
     * 生产管理->>带钢温度监控->>获取电磁感应入口温度
     * @param equipmentVariables
     * @return
     */
    @Override
    public StripTemMonitorVO getStrpTempVo(EquipmentVariables equipmentVariables) {
        CommentUtil.resetCompanyId(equipmentVariables);
        List<EquipmentVariables> strpTempVo = equipmentVariablesMapper.getStrpTempVo(equipmentVariables);
        List<Double> valueList = strpTempVo.stream()
                .map(EquipmentVariables::getVariableValue)
                .mapToDouble(Double::parseDouble)
                .boxed()
                .collect(Collectors.toList());
        return new StripTemMonitorVO(valueList);
    }

    /**
     * 生产管理->>激光扫描->>获取激光扫描结果
     * @param equipmentVariables
     * @return
     */
    @Override
    public List<String> getLaserScanResult(EquipmentVariables equipmentVariables) {
        List<String> result = new ArrayList<>();
        List<String> files=new ArrayList<String>(){{add("/root/rezha/base64.txt");add("/root/rezha/base641.txt");}};
        for (String file : files) {
            StringBuilder append = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    append.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            result.add(append.toString());
        }
        return result;

    }

//    /**
//     * 查询存储设备变量参数信息的列表
//     *
//     * @param equipmentVariables 存储设备变量参数信息的
//     * @return 存储设备变量参数信息的集合
//     */
//    @Override
//    public List<EquipVariablesVO> selectVariablesList(EquipmentVariables equipmentVariables) {
//        return equipmentVariablesMapper.selectVariablesList(equipmentVariables);
//    }
//
//    /**
//     * 导入点位数据
//     * @param coldefinetableList
//     * @param updateSupport
//     * @return
//     */
//    @Override
//    public String importVariablesVO(List<EquipVariablesVO> coldefinetableList, boolean updateSupport) {
//        if (coldefinetableList == null || coldefinetableList.size() == 0)
//        {
//            throw new RuntimeException("导入点位数据不能为空！");
//        }
//        int successCount = 0;
//        int failureCount = 0;
//        StringBuilder successMsg = new StringBuilder();
//        StringBuilder failureMsg = new StringBuilder();
//        for (EquipVariablesVO coldefinetable : coldefinetableList)
//        {
//            try
//            {
//                if (StringUtils.isBlank(coldefinetable.getPlcIp())
//                        || StringUtils.isBlank(coldefinetable.getPlcSrc())
//                        || StringUtils.isBlank(coldefinetable.getVariableName()))
//                {
//                    continue;
//                }
//                // 验证是否存在这个点位
//                EquipVariablesVO equipVariablesVo = equipmentVariablesMapper
//                        .getEquipVariablesVo(new EquipmentVariables(coldefinetable.getVariableName()));
//
//                if (equipVariablesVo == null)
//                {
//                    equipmentVariablesMapper.insertColdefinetable(coldefinetable);
//                    successCount++;
//                    successMsg.append("<br/>" + successCount + "、点位 " + coldefinetable.getVariableName() + " 导入成功");
//                }
//                else if (updateSupport)
//                {
//                    equipmentVariablesMapper.updateColdefinetable(coldefinetable);
//                    successCount++;
//                    successMsg.append("<br/>" + successCount + "、点位 " + coldefinetable.getVariableName() + " 更新成功");
//                }
//                else
//                {
//                    failureCount++;
//                    failureMsg.append("<br/>" + failureCount + "、点位 " + coldefinetable.getVariableName() + " 已存在");
//                }
//            }
//            catch (Exception e)
//            {
//                failureCount++;
//                String msg = "<br/>" + failureCount + "、点位 " + coldefinetable.getVariableName() + " 导入失败：";
//                failureMsg.append(msg + e.getMessage());
//            }
//        }
//        if (failureCount > 0)
//        {
//            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureCount + " 条数据格式不正确，错误如下：");
//            throw new RuntimeException(failureMsg.toString());
//        }
//        else
//        {
//            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successCount + " 条，数据如下：");
//        }
//        return successMsg.toString();
//    }

}
